Load all required libraries.

library(tidyverse)
## -- Attaching packages --------------------------------------- tidyverse 1.3.1 --
## v ggplot2 3.3.5     v purrr   0.3.4
## v tibble  3.1.6     v dplyr   1.0.8
## v tidyr   1.2.0     v stringr 1.4.0
## v readr   2.1.2     v forcats 0.5.1
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
library(plotly)
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
library(broom)

Read in raw data from RDS.

raw_data <- readRDS("./n1_n2_cleaned_cases.rds")

Make a few small modifications to names and data for visualizations.

final_data <- raw_data %>% mutate(log_copy_per_L = log10(mean_copy_num_L)) %>%
  rename(Facility = wrf) %>%
  mutate(Facility = recode(Facility, 
                           "NO" = "WRF A",
                           "MI" = "WRF B",
                           "CC" = "WRF C"))

Seperate the data by gene target to ease layering in the final plot

#make three data layers
only_positives <<- subset(final_data, (!is.na(final_data$Facility)))
only_n1 <- subset(only_positives, target == "N1")
only_n2 <- subset(only_positives, target == "N2")
only_background <<-final_data %>% 
  select(c(date, cases_cum_clarke, new_cases_clarke, X7_day_ave_clarke)) %>%
  group_by(date) %>% summarise_if(is.numeric, mean)

#specify fun colors
background_color <- "#7570B3"
seven_day_ave_color <- "#E6AB02"
marker_colors <- c("N1" = '#1B9E77',"N2" ='#D95F02')
#remove facilty C for now
#only_n1 <- only_n1[!(only_n1$Facility == "WRF C"),]
#only_n2 <- only_n2[!(only_n2$Facility == "WRF C"),]

only_n1 <- only_n1[!(only_n1$Facility == "WRF A" & only_n1$date == "2020-11-02"), ]
only_n2 <- only_n2[!(only_n2$Facility == "WRF A" & only_n2$date == "2020-11-02"), ]

Build the main plot

      #first layer is the background epidemic curve
        p1 <- only_background %>%
              plotly::plot_ly() %>%
              plotly::add_trace(x = ~date, y = ~new_cases_clarke, 
                                type = "bar", 
                                hoverinfo = "text",
                                text = ~paste('</br> Date: ', date,
                                                     '</br> Daily Cases: ', new_cases_clarke),
                                alpha = 0.5,
                                name = "Daily Reported Cases",
                                color = background_color,
                                colors = background_color,
                                showlegend = FALSE) %>%
            layout(yaxis = list(title = "Clarke County Daily Cases", showline=TRUE)) %>%
            layout(legend = list(orientation = "h", x = 0.2, y = -0.3))
        
        #renders the main plot layer two as seven day moving average
        p1 <- p1 %>% plotly::add_trace(x = ~date, y = ~X7_day_ave_clarke, 
                             type = "scatter",
                             mode = "lines",
                             hoverinfo = "text",
                            text = ~paste('</br> Date: ', date,
                                                     '</br> Seven-Day Moving Average: ', X7_day_ave_clarke),
                             name = "Seven Day Moving Average Athens",
                             line = list(color = seven_day_ave_color),
                             showlegend = FALSE)
      

        
        #renders the main plot layer three as positive target hits
        
        p2 <- plotly::plot_ly() %>%
          plotly::add_trace(x = ~date, y = ~mean_copy_num_L,
                                       type = "scatter",
                                       mode = "markers",
                                       hoverinfo = "text",
                                       text = ~paste('</br> Date: ', date,
                                                     '</br> Facility: ', Facility,
                                                     '</br> Target: ', target,
                                                     '</br> Copies/L: ', round(mean_copy_num_L, digits = 2)),
                                       data = only_n1,
                                       symbol = ~Facility,
                                       marker = list(color = '#1B9E77', size = 8, opacity = 0.65),
                                       showlegend = FALSE) %>%
          plotly::add_trace(x = ~date, y = ~mean_copy_num_L,
                                       type = "scatter",
                                       mode = "markers",
                                       hoverinfo = "text",
                                       text = ~paste('</br> Date: ', date,
                                                     '</br> Facility: ', Facility,
                                                     '</br> Target: ', target,
                                                     '</br> Copies/L: ', round(mean_copy_num_L, digits = 2)),
                                       data = only_n2,
                                       symbol = ~Facility,
                                       marker = list(color = '#D95F02', size = 8, opacity = 0.65),
                                       showlegend = FALSE) %>%
            layout(yaxis = list(title = "SARS CoV-2 Copies/L", 
                                 showline = TRUE,
                                 type = "log",
                                 dtick = 1,
                                 automargin = TRUE)) %>%
            layout(legend = list(orientation = "h", x = 0.2, y = -0.3))
        
        #adds the limit of detection dashed line
        p2 <- p2 %>% plotly::add_segments(x = as.Date("2020-03-14"), 
                                          xend = ~max(date + 10), 
                                          y = 3571.429, yend = 3571.429,
                                          opacity = 0.35,
                                          line = list(color = "black", dash = "dash")) %>%
          layout(annotations = list(x = as.Date("2020-03-28"), y = 3.8, xref = "x", yref = "y", 
                                    text = "Limit of Detection", showarrow = FALSE))

        

        p1
        p2

Combine the two main plot pieces as a subplot

#seperate n1 and n2 frames by site
#n1
wrf_a_only_n1 <- subset(only_n1, Facility == "WRF A")
wrf_b_only_n1 <- subset(only_n1, Facility == "WRF B")
wrf_c_only_n1 <- subset(only_n1, Facility == "WRF C")

#n2
wrf_a_only_n2 <- subset(only_n2, Facility == "WRF A")
wrf_b_only_n2 <- subset(only_n2, Facility == "WRF B")
wrf_c_only_n2 <- subset(only_n2, Facility == "WRF C")


#rejoin the old data frames then seperate in to averages for each plant. 
wrfa_both <- full_join(wrf_a_only_n1, wrf_a_only_n2)%>%
  select(c(date, mean_total_copies)) %>%
  group_by(date) %>%
  summarize_if(is.numeric, mean) %>%
  ungroup() %>%
  mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke",
## "X7_day_ave_clarke", "Facility", "collection_num", "target",
## "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "mean_total_copies",
## "sd_total_copies", "log_copy_per_L")
wrfb_both <- full_join(wrf_b_only_n1, wrf_b_only_n2)%>%
  select(c(date, mean_total_copies)) %>%
  group_by(date) %>%
  summarize_if(is.numeric, mean) %>%
  ungroup() %>%
  mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke",
## "X7_day_ave_clarke", "Facility", "collection_num", "target",
## "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "mean_total_copies",
## "sd_total_copies", "log_copy_per_L")
wrfc_both <- full_join(wrf_c_only_n1, wrf_c_only_n2)%>%
  select(c(date, mean_total_copies)) %>%
  group_by(date) %>%
  summarize_if(is.numeric, mean) %>%
  ungroup() %>%
  mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke",
## "X7_day_ave_clarke", "Facility", "collection_num", "target",
## "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "mean_total_copies",
## "sd_total_copies", "log_copy_per_L")
#get max date
maxdate <- max(wrfa_both$date)
mindate <- min(wrfa_both$date)

Build loess smoothing figures figures

This makes the individual plots

#**************************************WRF A PLOT**********************************************
#add trendlines 
#extract data from geom_smooth
#both extract
# *********************************span 0.6***********************************
#*****************Must always update the n = TOTAL NUMBER OF DAYS*************************
extract_botha <- ggplot(wrfa_both, aes(x = date, y = log_total_copies_both)) + 
  stat_smooth(aes(outfit=fit_botha<<-..y..), method = "loess", color = '#1B9E77', 
              span = 0.25, n = 653)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_botha
## `geom_smooth()` using formula 'y ~ x'

fit_botha
##   [1] 12.94886 12.94917 12.94947 12.94975 12.95003 12.95029 12.95054 12.95077
##   [9] 12.95097 12.95116 12.95132 12.95146 12.95156 12.95164 12.95169 12.95170
##  [17] 12.95168 12.95161 12.95151 12.95137 12.95118 12.95095 12.95067 12.95034
##  [25] 12.94996 12.94952 12.94903 12.94848 12.94787 12.94720 12.94647 12.94567
##  [33] 12.94481 12.94387 12.94286 12.94178 12.94061 12.93933 12.93794 12.93646
##  [41] 12.93489 12.93323 12.93150 12.92970 12.92783 12.92590 12.92392 12.92190
##  [49] 12.91983 12.91773 12.91561 12.91346 12.91130 12.90913 12.90696 12.90480
##  [57] 12.90264 12.90050 12.89839 12.89630 12.89425 12.89225 12.89029 12.88838
##  [65] 12.88654 12.88477 12.88307 12.88144 12.87991 12.87847 12.87712 12.87561
##  [73] 12.87367 12.87133 12.86862 12.86557 12.86219 12.85851 12.85457 12.85038
##  [81] 12.84598 12.84138 12.83661 12.83171 12.82669 12.82158 12.81641 12.81120
##  [89] 12.80598 12.80077 12.79560 12.79050 12.78550 12.78061 12.77586 12.77128
##  [97] 12.76690 12.76274 12.75883 12.75519 12.75184 12.74883 12.74616 12.74387
## [105] 12.74198 12.74052 12.73913 12.73745 12.73553 12.73340 12.73110 12.72867
## [113] 12.72613 12.72354 12.72092 12.71832 12.71576 12.71329 12.71095 12.70876
## [121] 12.70678 12.70503 12.70355 12.70238 12.70155 12.70111 12.70109 12.70153
## [129] 12.70245 12.70391 12.70594 12.70857 12.71184 12.71579 12.72046 12.72672
## [137] 12.73526 12.74585 12.75824 12.77219 12.78745 12.80379 12.82096 12.83872
## [145] 12.85683 12.87504 12.89311 12.91081 12.92788 12.94409 12.95919 12.97295
## [153] 12.98511 12.99544 13.00600 13.01879 13.03347 13.04972 13.06719 13.08554
## [161] 13.10445 13.12358 13.14259 13.16115 13.17892 13.19557 13.21076 13.22415
## [169] 13.23542 13.24422 13.25230 13.26155 13.27184 13.28305 13.29503 13.30766
## [177] 13.32083 13.33438 13.34821 13.36218 13.37616 13.39002 13.40364 13.41689
## [185] 13.42963 13.44174 13.45310 13.46357 13.47303 13.48134 13.48838 13.49402
## [193] 13.49813 13.50059 13.50126 13.50002 13.49674 13.49128 13.48366 13.47404
## [201] 13.46260 13.44947 13.43482 13.41879 13.40155 13.38323 13.36401 13.34402
## [209] 13.32344 13.30239 13.28106 13.25957 13.23810 13.21678 13.19579 13.17526
## [217] 13.15536 13.13330 13.10664 13.07598 13.04193 13.00510 12.96612 12.92560
## [225] 12.88414 12.84237 12.80089 12.76032 12.72127 12.68436 12.65020 12.61940
## [233] 12.59257 12.56655 12.53797 12.50719 12.47456 12.44041 12.40511 12.36900
## [241] 12.33243 12.29574 12.25929 12.22342 12.18849 12.15484 12.12281 12.09277
## [249] 12.06505 12.04000 12.01798 11.99933 11.98394 11.97126 11.96097 11.95279
## [257] 11.94640 11.94151 11.93780 11.93499 11.93276 11.93080 11.92883 11.92653
## [265] 11.92361 11.91975 11.91466 11.90803 11.90191 11.89835 11.89706 11.89774
## [273] 11.90011 11.90385 11.90870 11.91434 11.92048 11.92684 11.93312 11.93901
## [281] 11.94425 11.94851 11.95152 11.95298 11.95260 11.95008 11.94512 11.93952
## [289] 11.93512 11.93175 11.92920 11.92730 11.92586 11.92469 11.92360 11.92241
## [297] 11.92093 11.91897 11.91635 11.91288 11.90837 11.90263 11.89548 11.88656
## [305] 11.87575 11.86325 11.84927 11.83400 11.81763 11.80038 11.78244 11.76401
## [313] 11.74528 11.72646 11.70774 11.68932 11.67141 11.65420 11.63789 11.62268
## [321] 11.60876 11.59635 11.58252 11.56466 11.54340 11.51939 11.49327 11.46567
## [329] 11.43723 11.40859 11.38039 11.35327 11.32786 11.30481 11.28476 11.26834
## [337] 11.25619 11.24895 11.24455 11.24054 11.23699 11.23393 11.23144 11.22957
## [345] 11.22838 11.22792 11.22826 11.22944 11.23154 11.23459 11.23867 11.24383
## [353] 11.25012 11.25761 11.26635 11.27640 11.28782 11.30143 11.31787 11.33694
## [361] 11.35841 11.38207 11.40771 11.43511 11.46406 11.49434 11.52574 11.55805
## [369] 11.59104 11.62451 11.65824 11.69202 11.72562 11.75884 11.79147 11.82328
## [377] 11.85406 11.88360 11.91168 11.93809 11.96554 11.99657 12.03074 12.06761
## [385] 12.10674 12.14768 12.18999 12.23322 12.27695 12.32071 12.36407 12.40659
## [393] 12.44781 12.48731 12.52464 12.55934 12.59099 12.61914 12.64334 12.66641
## [401] 12.69122 12.71741 12.74462 12.77249 12.80065 12.82874 12.85640 12.88327
## [409] 12.90898 12.93317 12.95549 12.97557 12.99304 13.00754 13.01872 13.02680
## [417] 13.03235 13.03557 13.03665 13.03579 13.03317 13.02898 13.02342 13.01667
## [425] 13.00892 13.00038 12.99122 12.98164 12.97183 12.96198 12.95228 12.94292
## [433] 12.93409 12.92599 12.91880 12.91000 12.89731 12.88128 12.86243 12.84131
## [441] 12.81846 12.79441 12.76971 12.74490 12.72052 12.69710 12.67519 12.65533
## [449] 12.63805 12.62389 12.61021 12.59419 12.57611 12.55627 12.53497 12.51250
## [457] 12.48914 12.46520 12.44096 12.41672 12.39277 12.36941 12.34692 12.32560
## [465] 12.30574 12.28764 12.27159 12.25787 12.24679 12.23746 12.22878 12.22071
## [473] 12.21321 12.20624 12.19977 12.19376 12.18818 12.18298 12.17812 12.17358
## [481] 12.16931 12.16528 12.16145 12.15777 12.15422 12.15101 12.14836 12.14627
## [489] 12.14472 12.14371 12.14323 12.14327 12.14383 12.14489 12.14644 12.14848
## [497] 12.15100 12.15399 12.15743 12.16133 12.16568 12.17045 12.17565 12.18127
## [505] 12.18812 12.19689 12.20739 12.21945 12.23288 12.24750 12.26312 12.27956
## [513] 12.29664 12.31417 12.33198 12.34986 12.36765 12.38516 12.40221 12.41861
## [521] 12.43417 12.44873 12.46208 12.47405 12.48446 12.49549 12.50924 12.52537
## [529] 12.54357 12.56350 12.58484 12.60726 12.63044 12.65405 12.67776 12.70126
## [537] 12.72420 12.74627 12.76714 12.78648 12.80397 12.81928 12.83208 12.84205
## [545] 12.84887 12.85219 12.85364 12.85498 12.85616 12.85712 12.85780 12.85814
## [553] 12.85807 12.85755 12.85650 12.85487 12.85260 12.84962 12.84589 12.84134
## [561] 12.83590 12.82952 12.82214 12.81370 12.80414 12.79340 12.78142 12.76624
## [569] 12.74638 12.72243 12.69501 12.66474 12.63222 12.59806 12.56289 12.52730
## [577] 12.49191 12.45733 12.42418 12.39306 12.36459 12.33938 12.31804 12.29747
## [585] 12.27439 12.24914 12.22207 12.19352 12.16384 12.13337 12.10246 12.07144
## [593] 12.04068 12.01050 11.98125 11.95328 11.92694 11.90256 11.88049 11.86108
## [601] 11.84467 11.83160 11.82003 11.80797 11.79559 11.78302 11.77043 11.75797
## [609] 11.74579 11.73404 11.72288 11.71246 11.70292 11.69444 11.68714 11.68120
## [617] 11.67676 11.67398 11.67226 11.67096 11.67010 11.66973 11.66987 11.67058
## [625] 11.67187 11.67380 11.67639 11.67968 11.68371 11.68852 11.69414 11.70060
## [633] 11.70795 11.71622 11.72545 11.73567 11.74692 11.75936 11.77308 11.78804
## [641] 11.80419 11.82147 11.83984 11.85924 11.87962 11.90093 11.92311 11.94613
## [649] 11.96992 11.99444 12.01963 12.04544 12.07183
#assign fits to a vector
both_trenda <- fit_botha

#extract y min and max for each
limits_botha <- ggplot_build(extract_botha)$data
## `geom_smooth()` using formula 'y ~ x'
limits_botha <- as.data.frame(limits_botha)
both_ymina <- limits_botha$ymin
both_ymaxa <- limits_botha$ymax

#reassign dataframes (just to be safe)
work_botha <- wrfa_both

#fill in missing dates to smooth fits
work_botha <- work_botha %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_botha <- work_botha$date

#create a new smooth dataframe to layer
smooth_frame_botha <- data.frame(date_vec_botha, both_trenda, both_ymina, both_ymaxa)
#WRF A
#plot smooth frames
p_wrf_a <- plotly::plot_ly() %>%
  plotly::add_lines(x = ~date_vec_botha, y = ~both_trenda,
                    data = smooth_frame_botha,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_botha,
                                  '</br> Median Log Copies: ', round(both_trenda, digits = 2)),
                    line = list(color = '#1B9E77', size = 8, opacity = 0.65),
                    showlegend = FALSE) %>%
     layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_botha, ymin = ~both_ymina, ymax = ~both_ymaxa,
                    showlegend = FALSE,
                    opacity = 0.25,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_botha, #leaving in case we want to change
                                  '</br> Max Log Copies: ', round(both_ymaxa, digits = 2),
                                  '</br> Min Log Copies: ', round(both_ymina, digits = 2)),
                    name = "",
                    fillcolor = '#1B9E77',
                    line = list(color = '#1B9E77')) %>%
                layout(yaxis = list(title = "Total Log10 SARS CoV-2 Copies", 
                                 showline = TRUE,
                                 automargin = TRUE)) %>%
                layout(xaxis = list(title = "Date")) %>%
                layout(title = "WRF A") %>%
  plotly::add_markers(x = ~date, y = ~log_total_copies_both,
                      data = wrfa_both,
                       hoverinfo = "text",
                       showlegend = FALSE,
                       text = ~paste('</br> Date: ', date, 
                                     '</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
                       marker = list(color = '#1B9E77', size = 6, opacity = 0.65))

p_wrf_a
save(p_wrf_a, file = "./plotly_objs/p_wrf_a.rda")
#**************************************WRF B PLOT**********************************************
#add trendlines 
#extract data from geom_smooth
#both extract
# *********************************span 0.6***********************************
#*****************Must always update the n = TOTAL NUMBER OF DAYS*************************
extract_bothb <- ggplot(wrfb_both, aes(x = date, y = log_total_copies_both)) + 
  stat_smooth(aes(outfit=fit_bothb<<-..y..), method = "loess", color = '#D95F02', 
              span = 0.25, n = 653)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_bothb
## `geom_smooth()` using formula 'y ~ x'

fit_bothb
##   [1] 12.59104 12.58815 12.58534 12.58260 12.57994 12.57734 12.57482 12.57236
##   [9] 12.56996 12.56763 12.56536 12.56315 12.56100 12.55890 12.55686 12.55488
##  [17] 12.55294 12.55105 12.54921 12.54742 12.54567 12.54396 12.54229 12.54067
##  [25] 12.53908 12.53752 12.53600 12.53451 12.53305 12.53162 12.53021 12.52883
##  [33] 12.52748 12.52614 12.52483 12.52353 12.52222 12.52086 12.51946 12.51804
##  [41] 12.51658 12.51511 12.51363 12.51215 12.51067 12.50920 12.50774 12.50631
##  [49] 12.50491 12.50355 12.50223 12.50096 12.49974 12.49860 12.49752 12.49652
##  [57] 12.49561 12.49479 12.49406 12.49345 12.49294 12.49255 12.49229 12.49216
##  [65] 12.49218 12.49233 12.49264 12.49312 12.49375 12.49456 12.49556 12.49666
##  [73] 12.49781 12.49899 12.50023 12.50151 12.50285 12.50423 12.50568 12.50718
##  [81] 12.50874 12.51037 12.51206 12.51382 12.51565 12.51755 12.51953 12.52159
##  [89] 12.52373 12.52595 12.52826 12.53066 12.53315 12.53573 12.53840 12.54118
##  [97] 12.54405 12.54703 12.55011 12.55331 12.55661 12.56002 12.56356 12.56720
## [105] 12.57097 12.57487 12.57856 12.58175 12.58448 12.58681 12.58878 12.59044
## [113] 12.59184 12.59302 12.59402 12.59491 12.59572 12.59650 12.59730 12.59816
## [121] 12.59914 12.60028 12.60162 12.60322 12.60512 12.60737 12.61002 12.61311
## [129] 12.61669 12.62081 12.62551 12.63084 12.63685 12.64457 12.65478 12.66715
## [137] 12.68138 12.69715 12.71413 12.73201 12.75048 12.76921 12.78789 12.80621
## [145] 12.82383 12.84046 12.85576 12.86943 12.88114 12.89351 12.90913 12.92760
## [153] 12.94854 12.97156 12.99629 13.02232 13.04928 13.07679 13.10445 13.13187
## [161] 13.15868 13.18449 13.20892 13.23157 13.25206 13.27000 13.28502 13.29672
## [169] 13.30688 13.31749 13.32844 13.33967 13.35108 13.36259 13.37410 13.38554
## [177] 13.39682 13.40785 13.41855 13.42883 13.43860 13.44778 13.45628 13.46402
## [185] 13.47091 13.47686 13.48179 13.48561 13.48824 13.48958 13.48956 13.48809
## [193] 13.48508 13.48044 13.47409 13.46595 13.45514 13.44111 13.42421 13.40483
## [201] 13.38332 13.36005 13.33539 13.30972 13.28339 13.25678 13.23026 13.20418
## [209] 13.17893 13.15486 13.13235 13.11176 13.09025 13.06500 13.03638 13.00478
## [217] 12.97058 12.93416 12.89591 12.85622 12.81547 12.77403 12.73230 12.69066
## [225] 12.64949 12.60918 12.57011 12.53266 12.49721 12.46416 12.43389 12.40677
## [233] 12.38319 12.36086 12.33736 12.31289 12.28767 12.26189 12.23575 12.20948
## [241] 12.18326 12.15730 12.13181 12.10700 12.08306 12.06021 12.03864 12.01857
## [249] 12.00019 11.98371 11.96935 11.95729 11.94817 11.94221 11.93905 11.93831
## [257] 11.93962 11.94261 11.94690 11.95211 11.95788 11.96383 11.96958 11.97477
## [265] 11.97902 11.98195 11.98320 11.98239 11.98238 11.98600 11.99282 12.00242
## [273] 12.01437 12.02825 12.04363 12.06007 12.07717 12.09448 12.11159 12.12807
## [281] 12.14348 12.15741 12.16943 12.17911 12.18602 12.18974 12.18985 12.18828
## [289] 12.18717 12.18640 12.18584 12.18537 12.18485 12.18417 12.18318 12.18178
## [297] 12.17982 12.17719 12.17375 12.16939 12.16396 12.15736 12.14944 12.13911
## [305] 12.12557 12.10919 12.09029 12.06924 12.04636 12.02202 11.99656 11.97031
## [313] 11.94363 11.91687 11.89037 11.86447 11.83953 11.81588 11.79388 11.77387
## [321] 11.75619 11.74120 11.72581 11.70708 11.68553 11.66171 11.63615 11.60939
## [329] 11.58197 11.55441 11.52725 11.50104 11.47631 11.45358 11.43341 11.41632
## [337] 11.40285 11.39353 11.38639 11.37916 11.37195 11.36488 11.35807 11.35161
## [345] 11.34564 11.34025 11.33557 11.33171 11.32879 11.32691 11.32619 11.32674
## [353] 11.32868 11.33212 11.33718 11.34396 11.35259 11.36384 11.37825 11.39558
## [361] 11.41559 11.43803 11.46265 11.48922 11.51748 11.54720 11.57812 11.61001
## [369] 11.64261 11.67570 11.70901 11.74230 11.77534 11.80788 11.83966 11.87045
## [377] 11.90001 11.92809 11.95443 11.97881 12.00359 12.03110 12.06098 12.09291
## [385] 12.12654 12.16154 12.19756 12.23427 12.27133 12.30840 12.34514 12.38121
## [393] 12.41627 12.44999 12.48202 12.51203 12.53968 12.56462 12.58653 12.60765
## [401] 12.63028 12.65412 12.67887 12.70425 12.72996 12.75570 12.78119 12.80612
## [409] 12.83021 12.85316 12.87468 12.89447 12.91224 12.92771 12.94056 12.95137
## [417] 12.96093 12.96929 12.97650 12.98260 12.98765 12.99170 12.99479 12.99698
## [425] 12.99831 12.99884 12.99861 12.99768 12.99609 12.99389 12.99113 12.98787
## [433] 12.98414 12.98001 12.97551 12.96958 12.96127 12.95087 12.93864 12.92486
## [441] 12.90980 12.89373 12.87693 12.85966 12.84220 12.82483 12.80782 12.79143
## [449] 12.77594 12.76163 12.74625 12.72764 12.70617 12.68223 12.65621 12.62848
## [457] 12.59942 12.56943 12.53888 12.50816 12.47764 12.44771 12.41876 12.39117
## [465] 12.36531 12.34157 12.32034 12.30199 12.28691 12.27268 12.25683 12.23966
## [473] 12.22149 12.20264 12.18342 12.16414 12.14512 12.12666 12.10909 12.09271
## [481] 12.07784 12.06479 12.05387 12.04541 12.03970 12.03568 12.03207 12.02890
## [489] 12.02620 12.02399 12.02231 12.02119 12.02066 12.02073 12.02145 12.02284
## [497] 12.02492 12.02774 12.03131 12.03567 12.04085 12.04686 12.05375 12.06154
## [505] 12.07162 12.08515 12.10178 12.12122 12.14312 12.16717 12.19303 12.22040
## [513] 12.24894 12.27832 12.30824 12.33835 12.36834 12.39788 12.42665 12.45432
## [521] 12.48057 12.50508 12.52752 12.54757 12.56490 12.58272 12.60415 12.62877
## [529] 12.65614 12.68586 12.71747 12.75057 12.78472 12.81949 12.85446 12.88920
## [537] 12.92328 12.95628 12.98777 13.01732 13.04451 13.06890 13.09007 13.10760
## [545] 13.12105 13.13000 13.13616 13.14152 13.14608 13.14982 13.15273 13.15481
## [553] 13.15604 13.15642 13.15592 13.15455 13.15230 13.14915 13.14509 13.14011
## [561] 13.13421 13.12737 13.11958 13.11083 13.10112 13.09044 13.07876 13.06442
## [569] 13.04606 13.02417 12.99926 12.97180 12.94229 12.91122 12.87908 12.84637
## [577] 12.81358 12.78119 12.74970 12.71961 12.69139 12.66555 12.64258 12.61977
## [585] 12.59433 12.56658 12.53684 12.50546 12.47275 12.43905 12.40468 12.36998
## [593] 12.33526 12.30086 12.26711 12.23433 12.20285 12.17301 12.14512 12.11953
## [601] 12.09655 12.07651 12.05767 12.03813 12.01804 11.99756 11.97681 11.95596
## [609] 11.93513 11.91448 11.89415 11.87429 11.85503 11.83653 11.81893 11.80237
## [617] 11.78699 11.77295 11.75969 11.74659 11.73369 11.72101 11.70860 11.69648
## [625] 11.68468 11.67325 11.66222 11.65162 11.64148 11.63183 11.62272 11.61417
## [633] 11.60622 11.59890 11.59225 11.58630 11.58107 11.57675 11.57342 11.57103
## [641] 11.56955 11.56891 11.56908 11.56999 11.57161 11.57388 11.57675 11.58018
## [649] 11.58411 11.58850 11.59329 11.59844 11.60389
#assign fits to a vector
both_trendb <- fit_bothb

#extract y min and max for each
limits_bothb <- ggplot_build(extract_bothb)$data
## `geom_smooth()` using formula 'y ~ x'
limits_bothb <- as.data.frame(limits_bothb)
both_yminb <- limits_bothb$ymin
both_ymaxb <- limits_bothb$ymax

#reassign dataframes (just to be safe)
work_bothb <- wrfb_both

#fill in missing dates to smooth fits
work_bothb <- work_bothb %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_bothb <- work_bothb$date

#create a new smooth dataframe to layer
smooth_frame_bothb <- data.frame(date_vec_bothb, both_trendb, both_yminb, both_ymaxb)
#WRF B
#plot smooth frames
p_wrf_b <- plotly::plot_ly() %>%
  plotly::add_lines(x = ~date_vec_bothb, y = ~both_trendb,
                    data = smooth_frame_bothb,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_bothb,
                                  '</br> Median Log Copies: ', round(both_trendb, digits = 2)),
                    line = list(color = '#D95F02', size = 8, opacity = 0.65),
                    showlegend = FALSE) %>%
     layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_bothb, ymin = ~both_yminb, ymax = ~both_ymaxb,
                    showlegend = FALSE,
                    opacity = 0.25,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_bothb, #leaving in case we want to change
                                  '</br> Max Log Copies: ', round(both_ymaxb, digits = 2),
                                  '</br> Min Log Copies: ', round(both_yminb, digits = 2)),
                    name = "",
                    fillcolor = '#D95F02',
                    line = list(color = '#D95F02')) %>%
                layout(yaxis = list(title = "Total Log10 SARS CoV-2 Copies", 
                                 showline = TRUE,
                                 automargin = TRUE)) %>%
                layout(xaxis = list(title = "Date")) %>%
                layout(title = "WRF B") %>%
  plotly::add_markers(x = ~date, y = ~log_total_copies_both,
                      data = wrfb_both,
                       hoverinfo = "text",
                       showlegend = FALSE,
                       text = ~paste('</br> Date: ', date, 
                                     '</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
                       marker = list(color = '#D95F02', size = 6, opacity = 0.65))

p_wrf_b
save(p_wrf_b, file = "./plotly_objs/p_wrf_b.rda")

#**************************************WRF C PLOT********************************************** #add trendlines #extract data from geom_smooth # *********************************span 0.6*********************************** #*****************Must always update the n = TOTAL NUMBER OF DAYS*************************

extract_bothc <- ggplot(wrfc_both, aes(x = date, y = log_total_copies_both)) + 
  stat_smooth(aes(outfit=fit_bothc<<-..y..), method = "loess", color = '#E7298A', 
              span = 0.25, n = 653)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_bothc
## `geom_smooth()` using formula 'y ~ x'

fit_bothc
##   [1] 11.90048 11.90132 11.90216 11.90298 11.90379 11.90459 11.90536 11.90611
##   [9] 11.90683 11.90753 11.90819 11.90882 11.90940 11.90995 11.91045 11.91090
##  [17] 11.91131 11.91166 11.91195 11.91218 11.91235 11.91245 11.91248 11.91244
##  [25] 11.91232 11.91213 11.91185 11.91149 11.91105 11.91051 11.90988 11.90915
##  [33] 11.90832 11.90739 11.90635 11.90520 11.90394 11.90257 11.90107 11.89946
##  [41] 11.89772 11.89585 11.89386 11.89164 11.88914 11.88637 11.88334 11.88008
##  [49] 11.87661 11.87294 11.86910 11.86509 11.86094 11.85668 11.85230 11.84785
##  [57] 11.84333 11.83876 11.83416 11.82955 11.82495 11.82037 11.81584 11.81137
##  [65] 11.80699 11.80271 11.79854 11.79452 11.79065 11.78696 11.78346 11.78017
##  [73] 11.77711 11.77430 11.77176 11.76951 11.76756 11.76593 11.76401 11.76120
##  [81] 11.75757 11.75318 11.74809 11.74236 11.73607 11.72928 11.72204 11.71442
##  [89] 11.70649 11.69831 11.68994 11.68144 11.67289 11.66434 11.65585 11.64750
##  [97] 11.63934 11.63144 11.62387 11.61667 11.60993 11.60370 11.59804 11.59303
## [105] 11.58871 11.58517 11.58245 11.58063 11.57977 11.57993 11.58118 11.58357
## [113] 11.58717 11.59210 11.59834 11.60581 11.61442 11.62407 11.63468 11.64616
## [121] 11.65841 11.67134 11.68487 11.69891 11.71335 11.72812 11.74311 11.75825
## [129] 11.77344 11.78858 11.80359 11.81838 11.83286 11.84694 11.86052 11.87569
## [137] 11.89437 11.91618 11.94073 11.96765 11.99655 12.02707 12.05883 12.09144
## [145] 12.12453 12.15773 12.19065 12.22291 12.25414 12.28397 12.31200 12.33788
## [153] 12.36121 12.38162 12.40204 12.42536 12.45113 12.47890 12.50822 12.53864
## [161] 12.56971 12.60099 12.63202 12.66236 12.69156 12.71916 12.74472 12.76779
## [169] 12.78793 12.80467 12.81983 12.83544 12.85140 12.86764 12.88404 12.90052
## [177] 12.91698 12.93333 12.94948 12.96532 12.98077 12.99573 13.01011 13.02381
## [185] 13.03674 13.04880 13.05991 13.06996 13.07886 13.08652 13.09285 13.09775
## [193] 13.10112 13.10287 13.10291 13.10114 13.09747 13.09181 13.08355 13.07233
## [201] 13.05839 13.04200 13.02340 13.00286 12.98063 12.95696 12.93211 12.90633
## [209] 12.87988 12.85301 12.82598 12.79904 12.77244 12.74645 12.72131 12.69729
## [217] 12.67463 12.64951 12.61849 12.58240 12.54207 12.49832 12.45199 12.40390
## [225] 12.35488 12.30576 12.25737 12.21054 12.16610 12.12486 12.08768 12.05536
## [233] 12.02875 12.00444 11.97863 11.95156 11.92346 11.89456 11.86510 11.83530
## [241] 11.80539 11.77562 11.74620 11.71738 11.68938 11.66244 11.63678 11.61264
## [249] 11.59025 11.56985 11.55165 11.53591 11.52283 11.51231 11.50403 11.49772
## [257] 11.49306 11.48978 11.48758 11.48616 11.48523 11.48449 11.48365 11.48243
## [265] 11.48051 11.47762 11.47345 11.46772 11.46332 11.46306 11.46646 11.47304
## [273] 11.48232 11.49382 11.50708 11.52162 11.53695 11.55260 11.56810 11.58297
## [281] 11.59673 11.60891 11.61903 11.62661 11.63119 11.63227 11.62939 11.62414
## [289] 11.61846 11.61238 11.60591 11.59909 11.59195 11.58450 11.57678 11.56881
## [297] 11.56062 11.55223 11.54368 11.53497 11.52615 11.51724 11.50827 11.49694
## [305] 11.48128 11.46179 11.43899 11.41336 11.38542 11.35567 11.32461 11.29274
## [313] 11.26058 11.22861 11.19734 11.16729 11.13894 11.11280 11.08939 11.06919
## [321] 11.05271 11.04046 11.02991 11.01834 11.00600 10.99313 10.97995 10.96673
## [329] 10.95368 10.94106 10.92909 10.91803 10.90810 10.89955 10.89261 10.88753
## [337] 10.88454 10.88389 10.88541 10.88870 10.89366 10.90015 10.90805 10.91724
## [345] 10.92760 10.93901 10.95135 10.96449 10.97832 10.99271 11.00754 11.02269
## [353] 11.03804 11.05347 11.06885 11.08406 11.09899 11.11490 11.13302 11.15318
## [361] 11.17520 11.19890 11.22409 11.25059 11.27824 11.30683 11.33621 11.36618
## [369] 11.39657 11.42719 11.45787 11.48843 11.51868 11.54845 11.57756 11.60582
## [377] 11.63306 11.65909 11.68374 11.70683 11.72987 11.75439 11.78018 11.80705
## [385] 11.83478 11.86320 11.89209 11.92125 11.95050 11.97962 12.00842 12.03670
## [393] 12.06427 12.09091 12.11644 12.14066 12.16336 12.18434 12.20341 12.22253
## [401] 12.24353 12.26610 12.28988 12.31454 12.33974 12.36513 12.39038 12.41514
## [409] 12.43908 12.46185 12.48312 12.50255 12.51979 12.53451 12.54636 12.55582
## [417] 12.56367 12.57000 12.57493 12.57856 12.58099 12.58234 12.58271 12.58221
## [425] 12.58093 12.57900 12.57651 12.57357 12.57028 12.56676 12.56311 12.55943
## [433] 12.55584 12.55243 12.54931 12.54499 12.53809 12.52896 12.51793 12.50535
## [441] 12.49154 12.47685 12.46161 12.44615 12.43082 12.41594 12.40186 12.38891
## [449] 12.37743 12.36775 12.35818 12.34691 12.33415 12.32007 12.30488 12.28878
## [457] 12.27194 12.25457 12.23687 12.21901 12.20120 12.18364 12.16650 12.14999
## [465] 12.13430 12.11963 12.10616 12.09409 12.08362 12.07369 12.06319 12.05225
## [473] 12.04097 12.02946 12.01783 12.00619 11.99465 11.98333 11.97233 11.96177
## [481] 11.95175 11.94239 11.93379 11.92607 11.91934 11.91281 11.90569 11.89811
## [489] 11.89019 11.88206 11.87386 11.86569 11.85769 11.84999 11.84271 11.83598
## [497] 11.82992 11.82466 11.82032 11.81704 11.81493 11.81412 11.81474 11.81692
## [505] 11.82057 11.82547 11.83153 11.83867 11.84678 11.85579 11.86559 11.87611
## [513] 11.88726 11.89893 11.91105 11.92353 11.93627 11.94918 11.96218 11.97517
## [521] 11.98807 12.00079 12.01324 12.02532 12.03694 12.05024 12.06712 12.08719
## [529] 12.11006 12.13534 12.16263 12.19154 12.22169 12.25267 12.28410 12.31559
## [537] 12.34673 12.37715 12.40644 12.43422 12.46009 12.48367 12.50455 12.52235
## [545] 12.53667 12.54713 12.55601 12.56578 12.57628 12.58735 12.59884 12.61058
## [553] 12.62243 12.63422 12.64580 12.65701 12.66769 12.67770 12.68686 12.69503
## [561] 12.70205 12.70776 12.71200 12.71462 12.71547 12.71437 12.71119 12.70484
## [569] 12.69468 12.68118 12.66481 12.64604 12.62535 12.60320 12.58008 12.55645
## [577] 12.53278 12.50955 12.48723 12.46629 12.44721 12.43045 12.41649 12.40316
## [585] 12.38812 12.37156 12.35369 12.33470 12.31479 12.29416 12.27301 12.25154
## [593] 12.22995 12.20844 12.18721 12.16645 12.14637 12.12716 12.10902 12.09216
## [601] 12.07677 12.06305 12.04986 12.03598 12.02153 12.00662 11.99135 11.97584
## [609] 11.96019 11.94453 11.92894 11.91356 11.89848 11.88381 11.86967 11.85616
## [617] 11.84340 11.83149 11.82002 11.80850 11.79698 11.78547 11.77400 11.76259
## [625] 11.75129 11.74010 11.72906 11.71820 11.70753 11.69710 11.68692 11.67702
## [633] 11.66742 11.65817 11.64927 11.64076 11.63267 11.62509 11.61809 11.61162
## [641] 11.60565 11.60015 11.59508 11.59040 11.58609 11.58210 11.57840 11.57495
## [649] 11.57172 11.56868 11.56578 11.56300 11.56030
#assign fits to a vector
both_trendc <- fit_bothc

#extract y min and max for each
limits_bothc <- ggplot_build(extract_bothc)$data
## `geom_smooth()` using formula 'y ~ x'
limits_bothc <- as.data.frame(limits_bothc)
both_yminc <- limits_bothc$ymin
both_ymaxc <- limits_bothc$ymax

#reassign dataframes (just to be safe)
work_bothc <- wrfc_both

#fill in missing dates to smooth fits
work_bothc <- work_bothc %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_bothc <- work_bothc$date

#create a new smooth dataframe to layer
smooth_frame_bothc <- data.frame(date_vec_bothc, both_trendc, both_yminc, both_ymaxc)
#WRF C
#plot smooth frames
p_wrf_c <- plotly::plot_ly() %>%
  plotly::add_lines(x = ~date_vec_bothc, y = ~both_trendc,
                    data = smooth_frame_bothc,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_bothc,
                                  '</br> Median Log Copies: ', round(both_trendc, digits = 2)),
                    line = list(color = '#E7298A', size = 8, opacity = 0.65),
                    showlegend = FALSE) %>%
     layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_bothc, ymin = ~both_yminc, ymax = ~both_ymaxc,
                    showlegend = FALSE,
                    opacity = 0.25,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_bothc, #leaving in case we want to change
                                  '</br> Max Log Copies: ', round(both_ymaxc, digits = 2),
                                  '</br> Min Log Copies: ', round(both_yminc, digits = 2)),
                    name = "",
                    fillcolor = '#E7298A',
                    line = list(color = '#E7298A')) %>%
                layout(yaxis = list(title = "Total Log10 SARS CoV-2 Copies", 
                                 showline = TRUE,
                                 automargin = TRUE)) %>%
                layout(xaxis = list(title = "Date")) %>%
                layout(title = "WRF C") %>%
  plotly::add_markers(x = ~date, y = ~log_total_copies_both,
                      data = wrfc_both,
                       hoverinfo = "text",
                       showlegend = FALSE,
                       text = ~paste('</br> Date: ', date, 
                                     '</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
                       marker = list(color = '#E7298A', size = 6, opacity = 0.65))

p_wrf_c
save(p_wrf_c, file = "./plotly_objs/p_wrf_c.rda")
save(wrfa_both, file = "./plotly_objs/wrfa_both.rda")
save(wrfb_both, file = "./plotly_objs/wrfb_both.rda")
save(wrfc_both, file = "./plotly_objs/wrfc_both.rda")
save(date_vec_botha, file = "./plotly_objs/date_vec_botha.rda")
save(date_vec_bothb, file = "./plotly_objs/date_vec_bothb.rda")
save(date_vec_bothc, file = "./plotly_objs/date_vec_bothc.rda")
save(both_ymina, file = "./plotly_objs/both_ymina.rda")
save(both_ymaxa, file = "./plotly_objs/both_ymaxa.rda")

save(both_yminb, file = "./plotly_objs/both_yminb.rda")
save(both_ymaxb, file = "./plotly_objs/both_ymaxb.rda")

save(both_yminc, file = "./plotly_objs/both_yminc.rda")
save(both_ymaxc, file = "./plotly_objs/both_ymaxc.rda")